package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.card.EntityXfMealTime;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.ConsTimeConfigOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfMealTime;
import com.ocom.payconfig.service.XfMealTimeService;
import com.ocom.payconfig.vo.DeleteMealVo;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class XfMealTimeServiceImpl extends ServiceImpl<IDaoXfMealTime, EntityXfMealTime> implements XfMealTimeService {

    @Autowired
    private IDaoXfMealTime daoXfMealTime;

    @Resource
    RedisUtil redisUtil;

    //2.2.获取餐段时间信息(getMealTimeInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityXfMealTime> GetMealTimeInfo(Long comId,ConsTimeConfigOnShowRequest request) {
        QueryWrapper<EntityXfMealTime> query = Wrappers.query();
        query.eq("xt.com_id",request.getComId());
        if (StringUtils.isNotNull(request.getAccTypeId())){
            query.eq("xt.acc_type_id",request.getAccTypeId());
        }
        if (StringUtils.isNotEmpty(request.getMealNo())){
            query.eq("xt.meal_no",request.getMealNo());
        }
        if (StringUtils.isNotNull(request.getPointId())){
            query.eq("xt.point_id",request.getPointId());
        }
        if (StringUtils.isNotNull(request.getConsTimeZone())){
            query.eq("xt.cons_time_zone",request.getConsTimeZone());
        }
        if(request.getPage()==null || request.getPage()==-1)request.setPage(1);
        if(request.getSize()==null || request.getSize()==-1)request.setSize(500);
        PageHelper.startPage(request.getPage(), request.getSize());
        return this.baseMapper.getList(query);
    }

    //2.4.添加餐段时间信息(addMealTimeInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Result AddMealTimeInfo(Long comId, EntityXfMealTime entityXfMealTime) {
        redisUtil.dels("xf_meal_time#comId:"+entityXfMealTime.getComId());
        if(entityXfMealTime.getPointId()>0 && entityXfMealTime.getAccTypeId()>0){
            LambdaQueryWrapper<EntityXfMealTime> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EntityXfMealTime::getComId,entityXfMealTime.getComId());
            queryWrapper.eq(EntityXfMealTime::getMealNo,entityXfMealTime.getMealNo());
            queryWrapper.eq(EntityXfMealTime::getPointId,entityXfMealTime.getPointId());
            queryWrapper.eq(EntityXfMealTime::getAccTypeId,0);
            queryWrapper.last("limit 1");
            EntityXfMealTime mealTime = this.getOne(queryWrapper);
            if(mealTime == null){
                return ResultUtil.error(ResultEnum.Err_4148);
            }
            if(mealTime.getBeginTime().compareTo(entityXfMealTime.getBeginTime())>0){
                return ResultUtil.error(ResultEnum.Err_4149);
            }
            if(mealTime.getEndTime().compareTo(entityXfMealTime.getEndTime())<0){
                return ResultUtil.error(ResultEnum.Err_4149);
            }
        }
        daoXfMealTime.insert(entityXfMealTime);
        return ResultUtil.success();
    }

    //2.6.修改餐段时间信息(updateMealTimeInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Result UpdateMealTimeInfo(Long comId, EntityXfMealTime entityXfMealTime) {
        redisUtil.dels("xf_meal_time#comId:"+entityXfMealTime.getComId());
        LambdaQueryWrapper<EntityXfMealTime> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(EntityXfMealTime::getComId,entityXfMealTime.getComId());
//        queryWrapper2.eq(EntityXfMealTime::getMealNo,entityXfMealTime.getMealNo());
        queryWrapper2.eq(EntityXfMealTime::getId,entityXfMealTime.getId());
        EntityXfMealTime xfMealTime =this.getOne(queryWrapper2);
        if(xfMealTime==null){
            return ResultUtil.error(ResultEnum.Err_3014);
        }
        if(xfMealTime.getPointId()>0 && xfMealTime.getAccTypeId()>0){
            LambdaQueryWrapper<EntityXfMealTime> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EntityXfMealTime::getComId,entityXfMealTime.getComId());
            queryWrapper.eq(EntityXfMealTime::getMealNo,xfMealTime.getMealNo());
            queryWrapper.eq(EntityXfMealTime::getPointId,xfMealTime.getPointId());
            queryWrapper.eq(EntityXfMealTime::getAccTypeId,0);
            queryWrapper.last("limit 1");
            EntityXfMealTime mealTime = this.getOne(queryWrapper);
            if(mealTime == null){
                return ResultUtil.error(ResultEnum.Err_4148);
            }
            if(mealTime.getBeginTime().compareTo(entityXfMealTime.getBeginTime())>0){
                return ResultUtil.error(ResultEnum.Err_4149);
            }
            if(mealTime.getEndTime().compareTo(entityXfMealTime.getEndTime())<0){
                return ResultUtil.error(ResultEnum.Err_4149);
            }
        }
        this.update(entityXfMealTime,queryWrapper2);
        return ResultUtil.success();
    }
    /**
     * 2.8.删除餐段时间信息(deleteMealTimeInfo)
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DeleteMealTimeInfo(Long comId, DeleteMealVo deleteMealVo) {
        redisUtil.dels("xf_meal_time#comId:"+deleteMealVo.getComId());
        LambdaQueryWrapper<EntityXfMealTime> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntityXfMealTime::getComId,deleteMealVo.getComId());
        queryWrapper.eq(EntityXfMealTime::getId,deleteMealVo.getId());
        EntityXfMealTime entityXfMealTime = this.getOne(queryWrapper);
        if(entityXfMealTime==null) return;
        if(entityXfMealTime.getPointId()==0 && entityXfMealTime.getAccTypeId()==0){
            LambdaQueryWrapper<EntityXfMealTime> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(EntityXfMealTime::getComId,deleteMealVo.getComId());
            queryWrapper2.eq(EntityXfMealTime::getMealNo,entityXfMealTime.getMealNo());
            this.remove(queryWrapper2);
            return;
        }
        this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfMealTime> getMealTimeList(Long companyId) {
        QueryWrapper<EntityXfMealTime> query = Wrappers.query();
        query.eq("com_id", companyId);
        return this.list(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE,companyId = "comId")
    public Integer getCount(Long comId, Integer pointId) {
        return this.count(Wrappers.lambdaQuery(EntityXfMealTime.class).eq(EntityXfMealTime::getComId,comId).eq(EntityXfMealTime::getPointId,pointId));
    }
}
